package com.blb.seckill.util;

import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.RateLimiter;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;


//@Component
//@Scope
//@Aspect
//public class AccessLimitAop {
//
//    @Autowired
//    private HttpServletResponse response;
//
//    private RateLimiter rateLimiter = RateLimiter.create(20.0);
//
//    @Pointcut("@annotation(com.blb.seckill.util.AccessLimit)")
//    public void limit(){}
//
//    @Around("limit()")
//    public Object around(ProceedingJoinPoint proceedingJoinPoint){
//        boolean flag = rateLimiter.tryAcquire();
//        Object obj = null;
//        try{
//            if (flag){
//                obj=proceedingJoinPoint.proceed();
//            }else{
//                response.setContentType("application/json;charset=UTF-8");
//                response.getWriter().print("Access Limited".getBytes("UTF-8"));
//            }
//        }catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        return obj;
//    }
//}
